package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.DepartmentDao;
import com.aaa.book_mall.entity.Department;
import com.aaa.book_mall.util.LayTab;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    //自动装载
    private DepartmentDao departmentDao;
    //获取页面信息
    @RequestMapping("/getAllDepartment")

    public LayTab getAllDepartment(Department department) {
         List<Map<String, Object>> allDempartment = departmentDao.getAllDempartment(department);
         int count = departmentDao.getBookCount(department);
        LayTab layTab = new LayTab(count, allDempartment);
        return layTab;

    }
    @RequestMapping("/insertDepartment")
    //修改和添加数据
    public Map insertDepartment(Department department){
        System.out.println("---");
        //如果传来的信息有deptid,证明是修改，否则就是新增
        int i=0;
        if (department.getDeptid()!=null || "".equals(department.getDeptid())){
            System.out.println("mao");
            i=departmentDao.updateDepartment(department);
        }else {
            i = departmentDao.insertDepartment(department);
            System.out.println("yun");
        }
        Map<Object, Object> map = new HashMap<>();
        if (i>0) {
            map.put("msg", "添加成功");
        }else {
            map.put("msg", "添加失败");
        }
        return map;
    }
    /**
     * 根据id删除正行的数据
     * @return
     */
    @RequestMapping("/delDeptidById")
    public Map<Object, Object> delDeptidById(Integer deptid){
      int i=departmentDao.delDeptidById(deptid);
      Map<Object, Object> map = new HashMap<>();
      if (i>0){
          map.put("msg", "添加成功");
      }else {
          map.put("msg", "添加失败");
      }
        return map;
    }

    @RequestMapping("/updateStatus")
    protected Map<Object, Object> updateStatus (Integer deptid, Integer status){
        int i = departmentDao.updateStatus(deptid, status);
        Map<Object, Object> map = new HashMap<>();
        if (i>0) {
            map.put("msg", "添加成功");
        }else {
            map.put("msg", "添加失败");
        }
        return map;
    }



}
